package gr.uoa.di.thanos.botcraft.client;

import java.io.PrintWriter;
import java.util.logging.Logger;

public class Sender implements Runnable {
	private static final Logger LOGGER = Logger.getLogger(Sender.class.getName());
	
	private final Client client;
	private final PrintWriter output;
	
	public Sender(final Client client, final PrintWriter output) {
		this.client = client;
		this.output = output;
	}
	
	@Override
	public void run() {
		while (!Thread.interrupted()) {
			final String command = client.issueCommand();
			if (command == null)
				Thread.currentThread().getThreadGroup().interrupt();
			else {
				output.println(command);
				output.flush();
				if (output.checkError()) {
					LOGGER.severe("Error sending command " + command + " to " + client);
					Thread.currentThread().getThreadGroup().interrupt();
				}
			}
		}
	}
}
